package com.zs.letcode.tencent;

/**
 * 合并两个有序链表
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * <p>
 * 输入：l1 = [1,2,4], l2 = [1,3,4]
 * 输出：[1,1,2,3,4,4]
 * 示例 2：
 * <p>
 * 输入：l1 = [], l2 = []
 * 输出：[]
 * 示例 3：
 * <p>
 * 输入：l1 = [], l2 = [0]
 * 输出：[0]
 * <p>
 * <p>
 * 提示：
 * <p>
 * 两个链表的节点数目范围是 [0, 50]
 * -100 <= Node.val <= 100
 * l1 和 l2 均按 非递减顺序 排列
 * 相关标签
 * 递归
 * 链表
 * <p>
 * 作者：力扣 (LeetCode)
 * 链接：https://leetcode-cn.com/leetbook/read/tencent/x59tp7/
 * 来源：力扣（LeetCode）
 * 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
 *
 * @author madison
 * @description
 * @date 2021/7/14 09:49
 */
public class Chapter21 {
    public static void main(String[] args) {

    }

    /**
     * Definition for singly-linked list.
     */
    private class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    private class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }
            ListNode head = null, tail = null;
            while (l1 != null || l2 != null) {
                if (l1 == null) {
                    if (head == null) {
                        head = tail = new ListNode(l2.val);
                    } else {
                        tail.next = new ListNode(l2.val);
                        tail = tail.next;
                    }
                    l2 = l2.next;
                } else if (l2 == null) {
                    if (head == null) {
                        head = tail = new ListNode(l1.val);
                    } else {
                        tail.next = new ListNode(l1.val);
                        tail = tail.next;
                    }
                    l1 = l1.next;
                } else {
                    int n1 = l1.val;
                    int n2 = l2.val;
                    if (head == null) {
                        head = tail = new ListNode(n1 > n2 ? n2 : n1);
                    } else {
                        tail.next = new ListNode(n1 > n2 ? n2 : n1);
                        tail = tail.next;
                    }
                    if (n1 > n2) {
                        l2 = l2.next;
                    } else {
                        l1 = l1.next;
                    }
                }
            }
            return head;
        }

        /**
         * 方法一：递归
         */
        public ListNode mergeTwoLists1(ListNode l1, ListNode l2) {
            if (l1 == null) {
                return l2;
            } else if (l2 == null) {
                return l1;
            } else if (l1.val < l2.val) {
                l1.next = mergeTwoLists1(l1.next, l2);
                return l1;
            } else {
                l2.next = mergeTwoLists1(l1, l2.next);
                return l2;
            }
        }

        /**
         * 方法二：迭代
         */
        public ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
            ListNode prehead = new ListNode(-1);
            ListNode pre = prehead;
            while (l1 != null && l2 != null) {
                if (l1.val <= l2.val) {
                    pre.next = l1;
                    l1 = l1.next;
                } else {
                    pre.next = l2;
                    l2 = l2.next;
                }
                pre = pre.next;
            }
            pre.next = l1 == null ? l2 : l1;
            return prehead.next;
        }
    }
}
